/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import org.tzconverter.AbstractManager;
import org.tzconverter.menu.Item;
import org.tzconverter.menu.MenuView;

/**
 * Manager (presenter) for the "main" triad.
 * 
 * 
 * @since Jun 17, 2009
 */
public class MainManager extends AbstractManager
{
  private final MainView view;
  private final MainModel model;
  
  public MainManager(MainView view, MenuView menuView, MainModel model)
  {
    super(menuView);
    
    this.view = view;
    this.model = model;
    
    registerListeners();
  }
  
  /**
   * Perform post-construction initialization tasks.  Called automatically
   * by the Spring container.
   */
  public void afterConstructed()
  {
    //enable/disable the translucency menu item based
    //on whether or not translucency is supported.
    menuView.setEnabled(Item.TRANSLUCENT, view.isTranslucencySupported());
    menuView.setEnabled(Item.OPACITY_LEVEL, view.isTranslucencySupported());
    
    // Load the default values by invoking the same code that the load
    // menu option does
    whenLoadClicked(); 
  }
  
  @Override
  protected void whenLoadClicked()
  {
    model.loadSettings();
    menuView.setState(Item.ALWAYS_ON_TOP, model.isAlwaysOnTop());
    menuView.setState(Item.TRANSLUCENT, model.isTranslucent());
    menuView.setEnabled(Item.OPACITY_LEVEL, model.isTranslucent());
  }
  
  @Override
  protected void whenLoadDefaultsClicked()
  {
    model.loadDefaultSettings();
    menuView.setState(Item.ALWAYS_ON_TOP, model.isAlwaysOnTop());
    menuView.setState(Item.TRANSLUCENT, model.isTranslucent());
    menuView.setEnabled(Item.OPACITY_LEVEL, model.isTranslucent());
  }

  @Override
  protected void whenSaveClicked()
  {
    model.saveSettings();
  }
  
  private void whenOpacityLevelClicked()
  {
    view.showOpacityLevelDialog();
  }
  
  private void whenAboutClicked()
  {
    view.showAboutDialog();
  }

  private void whenExitClicked()
  {
    view.close();
  }
  
  private void whenAlwaysOnTopMenuItemClicked(int stateChange)
  {
    boolean isAlwaysOnTop = stateChange == ItemEvent.SELECTED;
    view.setAlwaysOnTop(isAlwaysOnTop);
    model.setAlwaysOnTop(isAlwaysOnTop);
  }
  
  private void whenTranslucentMenuItemClicked(int stateChange)
  {
    boolean makeTranslucent = stateChange == ItemEvent.SELECTED &&
                              view.isTranslucencySupported();
    
    float level = 1.0f;
    if(makeTranslucent)
    {
      level = model.getOpacityLevel();
    }
    
    view.setOpacityLevel(level);
    menuView.setEnabled(Item.OPACITY_LEVEL, makeTranslucent);
    model.setTranslucent(makeTranslucent);
  }
  
  private void registerListeners()
  {    
    menuView.addActionListener(Item.OPACITY_LEVEL, new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        whenOpacityLevelClicked();
      }
    });

    menuView.addActionListener(Item.ABOUT, new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        whenAboutClicked();
      }
    });
    
    menuView.addActionListener(Item.EXIT, new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
        whenExitClicked();
      }
    });
    
    menuView.addItemListener(Item.ALWAYS_ON_TOP, new ItemListener() {
      public void itemStateChanged(ItemEvent e)
      {
        whenAlwaysOnTopMenuItemClicked(e.getStateChange());
      }
    });
    
    menuView.addItemListener(Item.TRANSLUCENT, new ItemListener() {
      public void itemStateChanged(ItemEvent e)
      {
        whenTranslucentMenuItemClicked(e.getStateChange());
      }
    });
  }
}
